const { name } = require('./package');
let targetUrl = 'http://192.168.18.86:9099/';
process.env.VUE_APP_PORT = 7777; //设置端口号
process.env.VUE_APP_NAME = name; //项目名称 很重要 要写
process.env.VUE_BASE_URL = `/${name}/`;
let containerId = `${name}-${Math.random(15)}${new Date().getTime()}`;
process.env.VUE_APP_CONTAINER = containerId;

// let url = process.env.NODE_ENV === 'production' ? 'http://192.168.18.86:9099/' : `http://localhost:${process.env.VUE_APP_PORT}`;


require('events').EventEmitter.defaultMaxListeners = 0
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const productionGzipExtensions = ['js', 'css', 'json', 'txt', 'html', 'ico', 'svg', 'less']
const webpack = require('webpack')
let transpile = [
    '@jiaminghi/*',
    /[/\\]node_modules[/\\](.+?)?ts-element(.*)[/\\]packages/,
    /[/\\]node_modules[/\\](.+?)?ts-element(.*)[/\\]lib/,
    '@antv/*',
    'ml-matrix/*',
    /[/\\]node_modules[/\\](.+?)?many-keys-map(.*)/,
    /[/\\]node_modules[/\\](.+?)?ts-bi-ui(.*)[/\\]lib/,
    /[/\\]node_modules[/\\](.+?)?js-base64(.*)/,
    /[/\\]node_modules[/\\](.+?)?crypto-js(.*)[/\\]enc-base64.js/,
    /[/\\]node_modules[/\\](.+?)?crypto-js(.*)[/\\]enc-base64url.js/,
    /[/\\]node_modules[/\\](.+?)?core-js(.*)[/\\]internals/
]
transpile = (process.env.NODE_ENV === 'development') ? ['@jiaminghi/*', /[/\\]node_modules[/\\](.+?)?js-base64(.*)/, /[/\\]node_modules[/\\](.+?)?crypto-js(.*)[/\\]enc-base64.js/, '@antv/*', /[/\\]node_modules[/\\](.+?)?crypto-js(.*)[/\\]enc-base64url.js/, /\\]node_modules[/\\](.+?)?many-keys-map(.*)/,
    /[/\\]node_modules[/\\](.+?)?ts-bi-ui(.*)[/\\]lib/
] : transpile

module.exports = {
    parallel:false,
    publicPath: `${process.env.VUE_BASE_URL}`, //打包基础路劲
    'devServer': {
        headers: {
            'Access-Control-Allow-Origin': '*',
        },
        'port': process.env.VUE_APP_PORT,
        'disableHostCheck': true,
        'historyApiFallback': true,
        'proxy': {
            '/ts-query': {
                'target': targetUrl,
                'changeOrigin': true,
                'secure': false,
                'pathRewrite': {
                    '^/ts-query': '/ts-query'
                }
            },
            '/user': {
                'target': targetUrl,
                'changeOrigin': true,
                'secure': false,
                'pathRewrite': {
                    '^/user': '/user'
                }
            },
            '/ts-data-assets': {
                'target': targetUrl,
                'changeOrigin': true,
                'secure': false,
                'pathRewrite': {
                    '^/ts-data-assets': '/ts-data-assets'
                }
            },
            '/ts-system': {
                'target': targetUrl,
                'changeOrigin': true,
                'secure': false,
                'pathRewrite': {
                    '^/ts-system': '/ts-system'
                }
            },
            '/ts-datacenter': {
                'target': targetUrl,
                'changeOrigin': true,
                'secure': false,
                'pathRewrite': {
                    '^/ts-datacenter': '/ts-datacenter'
                }
            },
            '/ts-dataset': {
                'target': targetUrl,
                'changeOrigin': true,
                'secure': false,
                'pathRewrite': {
                    '^/ts-dataset': '/ts-dataset'
                }
            },
            '/ts-hsb': {
                'target': targetUrl,
                'changeOrigin': true,
                'secure': false,
                'pathRewrite': {
                    '^/ts-hsb': '/ts-hsb'
                }
            },
            '/ts-plat-monitor': {
                'target': targetUrl,
                'changeOrigin': true,
                'secure': false,
                'pathRewrite': {
                    '^/ts-plat-monitor': '/ts-plat-monitor'
                }
            }
        }
    },
    'transpileDependencies': transpile,
    //打包的ts-element 兼容ie
    'runtimeCompiler': true,
    chainWebpack: config => {
        // 微应用打包之后 css 中的字体文件和图片加载 404
        // 小文件转 base64 ，大文件注入路径前缀
        const fontRule = config.module.rule('fonts');
        fontRule.uses.clear();
        fontRule
        .use('file-loader')
        .loader('file-loader')
        .options({
            name: 'fonts/[name].[hash:8].[ext]',
            publicPath: '/wzh_ba/',
        })
        .end();
        const imgRule = config.module.rule('images');
        imgRule.uses.clear();
        imgRule
        .use('file-loader')
        .loader('file-loader')
        .options({
            name: 'img/[name].[hash:8].[ext]',
            publicPath: '/wzh_ba/',
        })
        .end();

        config
            .plugin('html')
            .tap(args => {
                args[0].title = 'demo111'
                return args
            })

        if (process.env.use_analyzer) { // 分析
            config
                .plugin('webpack-bundle-analyzer')
                .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
        }
    },
    'productionSourceMap': false,
    'configureWebpack': config => {
        let plugins = [
            new CompressionWebpackPlugin({
                algorithm: 'gzip',
                test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
                threshold: 10240,
                minRatio: 0.8
            }),
            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery'
            })
        ]
        if (process.env.NODE_ENV !== 'development') {
            config.plugins = [...config.plugins, ...plugins]
        }
        let output = {
            ...config.output,
            library: `${name}`,
            libraryTarget: 'umd', // 把子应用打包成 umd 库格式
            jsonpFunction: `webpackJsonp_${name}`
        }
        config.output = {...config.output, ...output }
    }
};
